package com.md.common.entity;

import com.md.common.dto.page.ResponseQuery;
import com.md.common.exception.ApiErrorCode;
import com.md.common.exception.BaseErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述：API响应dto
 *
 * @author lujianping@oak168.com
 * @version 1.0
 * @date 2018-6-14 11:51
 * @since JDK 1.8
 */
public class ApiResponse implements Serializable{
    private static final long serialVersionUID = 1503538415196825963L;
    protected int code;//成功返回0
    protected String msg;//返回消息
    protected Object data = null;//返回结果

    public ApiResponse() {
    }

    public ApiResponse(int code, String msg, Object data) {
        super();
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    
    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public Object getData() {
        return data;
    }
    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 响应列表
     * @param list
     * @return
     */
    public static ApiResponse success(List<?> list){
        return get(ApiErrorCode.SUCCESSFUL, new ApiErrorCode().getErrorMsg(ApiErrorCode.SUCCESSFUL),list);
    }

    /**
     * 响应分页列表
     * @param pageData
     * @return
     */
    public static ApiResponse success(ResponseQuery pageData){
        return get(ApiErrorCode.SUCCESSFUL, new ApiErrorCode().getErrorMsg(ApiErrorCode.SUCCESSFUL),pageData);
    }

    /**
     * 无响应对象
     * @return
     */
    public static ApiResponse success(){
        return get(ApiErrorCode.SUCCESSFUL, new ApiErrorCode().getErrorMsg(ApiErrorCode.SUCCESSFUL));
    }

    /**
     * 响应对象
     * @param data
     * @return
     */
    public static ApiResponse success(Object data){
        return get(ApiErrorCode.SUCCESSFUL, new ApiErrorCode().getErrorMsg(ApiErrorCode.SUCCESSFUL),data);
    }

    public static ApiResponse success(String msg, Object data) {
        return get(ApiErrorCode.SUCCESSFUL, msg, data);
    }

    public static ApiResponse error(int code){
        String message = String.format(new BaseErrorCode().getErrorMsg(code), "");
        return get(code, message);
    }

    public static ApiResponse error(String msg) {
        return error(ApiErrorCode.SYSTEM_ERROR, msg);
    }

    public static ApiResponse error(int code, String msg){
        return get(code, msg);
    }

    public static ApiResponse error(int code, String msg, BizErrorReponse error){
        return get(code, msg, error);
    }

    private static ApiResponse get(int code, String msg){
        return new ApiResponse(code, msg, null);
    }

    private static ApiResponse get(int code, String msg, Object data){
        Map bizData = new HashMap<>();
        bizData.put("data", data);
        return new ApiResponse(code, msg, bizData);
    }

    private static ApiResponse get(int code, String msg, BizErrorReponse data){
        Map bizData = new HashMap<>();
        bizData.put("error", data);
        return new ApiResponse(code, msg, bizData);
    }

    private static ApiResponse get(int code, String msg, List<?> list){
        DataListReponse dataListReponse = new DataListReponse();
        if(CollectionUtils.isNotEmpty(list)){
            dataListReponse.setList(list);
        }
        return new ApiResponse(code, msg, dataListReponse);
    }

    private static ApiResponse get(int code, String msg, ResponseQuery pageData){
        PageDataReponse pageDataReponse = new PageDataReponse();
        if(CollectionUtils.isNotEmpty(pageData.getList())){
            pageDataReponse.setList(pageData.getList());
            pageDataReponse.setTotal(pageData.getTotal());
            pageDataReponse.setTotalPage((long)pageData.getTotalPage());
        }
        return new ApiResponse(code, msg, pageDataReponse);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}
